//
//  XSDataListViewController.m
//  小说
//
//  Created by WZH on 2018/5/28.
//  Copyright © 2018年 WZH. All rights reserved.
//

#import "XSDataListViewController.h"
#import "XSDataListTableViewCell.h"
#import "XSDataListTableViewCell+XSDataInfo.h"
#import "XSHttp.h"
#import "XSPlayVideoViewController.h"

@interface XSDataListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray <SmallModel *>*infoArray;

@end

@implementation XSDataListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"请求列表";
    if (self.startNumber.length == 0) {// 查看本地数据
        self.infoArray = [MemberModel unarchive].infoArray.mutableCopy;
    }else{// 进行请求，获取新的数据
        self.infoArray = [NSMutableArray array];
        for (int i = 0; i < self.forNumber.integerValue; i++) {
            __weak XSDataListViewController *weakSelf = self;
            [XSHttp httpWithStartNumber:@(self.startNumber.integerValue + i).stringValue backDataArray:^(SmallModel *array) {
                [weakSelf.infoArray addObject:array];
                [weakSelf.tableView reloadData];
            }];
        }
    }
    [self initUI];
    [self configUI];
}

#pragma mark 初始化UI控件
- (void)initUI{
    self.tableView = [UITableView new];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[XSDataListTableViewCell class] forCellReuseIdentifier:XSDataListTableViewCellId];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.hidesBottomBarWhenPushed = YES;
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

#pragma mark 为UI控件添加约束
- (void)configUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(self.mas_topLayoutGuide);
    }];
}

#pragma mark 导航右侧按钮点击事件
- (void)rightClick:(UIButton *)sender{
    for (SmallModel *model in self.infoArray) {
        [MemberModel addSmallModel:model];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XSDataListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XSDataListTableViewCellId];
    [cell configureForModel:self.infoArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XSPlayVideoViewController *vc = [[XSPlayVideoViewController alloc] init];
    vc.model = self.infoArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
